import request from '@/utils/request'

/**
 * 上传文件
 * @param {File} file 文件对象
 * @returns {Promise}
 */
export function uploadFile(file) {
  const formData = new FormData()
  formData.append('file', file)
  
  return request({
    url: '/attach/upload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 上传图片
 * @param {File} file 图片文件
 * @returns {Promise}
 */
export function uploadImage(file) {
  return uploadFile(file)
}

/**
 * 批量上传文件
 * @param {FileList} files 文件列表
 * @returns {Promise}
 */
export function uploadFiles(files) {
  const formData = new FormData()
  
  for (let i = 0; i < files.length; i++) {
    formData.append('files', files[i])
  }
  
  return request({
    url: '/attach/upload/batch',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}
